package servlet;

import pojo.User;
import util.SendMail;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收用户请求，封装成对象
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        User user = new User(username, password, email);

        //用户注册成功之后，给用户发一封邮件
        //使用线程来专门发送邮件，防止出现耗时，和网站注册人数较多的情况
        SendMail send = new SendMail(user);
        //启动线程，执行run方法发送邮件
        send.start();
        //这里写run()的话，得等邮件发送完了才跳转，用户体验贼差！！


        //注册用户
        req.setAttribute("message", "注册成功，我们已经发了一封带了注册信息的电子邮件，请查收！如网络不稳定，可能过会儿才能收到！");
        req.getRequestDispatcher("info.jsp").forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
